

# 地理编码与逆地理编码



import requests
from PIL import Image
from io import BytesIO



#地理编码
def geocode(key,address,city=None,batch="false",sig=None,output="json",callback=None):
    """地理编码"""
    geocode_url = "https://restapi.amap.com/v3/geocode/geo?parameters"
    params = {
        "key": key,
        "address": address,
        "city": city,
        "batch": batch,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    r = requests.get(geocode_url,params=params)
    results = r.json()
    return results


#逆地理编码
def regeocode(key,location,poitype=None,radius=1000,extensions="base",batch="false",roadlevel=None,sig=None,output="json",callback=None,homeorcorp=0):
    """将经纬度转换为详细结构化的地址，且返回附近周边的POI、AOI信息"""
    re_geocode_url = "https://restapi.amap.com/v3/geocode/regeo?parameters"
    params = {
        "key":key,
        "location":location,
        "poitype":poitype,
        "radiu":radius,
        "extensions":extensions,
        "batch":batch,
        "roadlevel":roadlevel,
        "sig":sig,
        "output":output,
        "callback":callback,
        "homeorcorp":homeorcorp
    }
    r = requests.get(re_geocode_url,params=params)
    results = r.json()
    return results


#路径规划




# 步行路径规划
def walking(key,origin,destination,sig=None,output="json",callback=None):
    """步行路径规划"""
    walking_url = "https://restapi.amap.com/v3/direction/walking?parameters"
    params = {
        "key": key,
        "origin": origin,
        "destination": destination,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    r = requests.get(walking_url,params=params)
    results = r.json()
    return results



# 公交路径规划
def busing(key,origin,destination,city=None,cityd=None,extensions="base",strategy=0,nightflag=0,date=None,time=None,sig=None,output="json",callback=None):
    """公交路径规划"""
    busing_url = "https://restapi.amap.com/v3/direction/transit/integrated?parameters"
    params = {
        "key": key,
        "origin": origin,
        "city": city,
        "cityd": cityd,
        "extensions": extensions,
        "strategy": strategy,
        "nightflag": nightflag,
        "date": date,
        "time": time,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    r = requests.get(transit_url,params=params)
    results = r.json()
    return results


# 驾车路径规划
def driving(key,origin,destination,originid=None,destinationid=None,origintype=None,destinationtype=None,strategy=0,waypoints=None,avoidpolygons=None,avoidroad=None,province=None,number=None,cartype=0,ferry=0,roadaggregation="false",nosteps=0,sig=None,output="json",callback=None,extensions="base"):
    """驾车路径规划"""
    driving_url = "https://restapi.amap.com/v3/direction/driving?parameters"
    params = {
        "key": key,
        "origin": origin,
        "destination": destination,
        "originid": originid,
        "destinationid": destinationid,
        "origintype": origintype,
        "destinationtype": destinationtype,
        "strategy": strategy,
        "waypoints": waypoints,
        "avoidpolygons": avoidpolygons,
        "avoidroad": avoidroad,
        "province": province,
        "number": number,
        "cartype": cartype,
        "ferry": ferry,
        "roadaggregation": roadaggregation,
        "nosteps": nosteps,
        "sig": sig,
        "output": output,
        "callback": callback,
        "extensions": extensions
    }
    r = requests.get(driving_url,params=params)
    results = r.json()
    return results



# 骑行路径规划
def bicycling(key,origin,destination,originid=None,destinationid=None,origintype=None,destinationtype=None):
    """骑行路径规划"""
    bicycling_url = "https://restapi.amap.com/v4/direction/bicycling?parameters"
    params = {
        "key": key,
        "origin": origin,
        "destination": destination,
        "destinationid": destinationid,
        "origintype": origintype,
        "destinationtype": destinationtype
    }
    r = requests.get(bicycling_url,params=params)
    results = r.json()
    return results



# 货车路径规划
def truck(key,origin,destination,originid=None,destinationid=None,origintype=None,destinationtype=None):
    """货车路径规划"""
    truck_url = "https://restapi.amap.com/v4/direction/truck?parameters"
    params = {
        "key": key,
        "origin": origin,
        "destination": destination,
        "destinationid": destinationid,
        "origintype": origintype,
        "destinationtype": destinationtype
    }
    r = requests.get(truck_url,params=params)
    results = r.json()
    return results


# 行政区域查询





def district(key,keywords,subdistric,page=1,offset=20,extensions="base",district_filter="adcode",callback=None,output="json"):
    """行政区域查询"""
    district_url = "https://restapi.amap.com/v3/config/district?parameters"
    params = {
        "key": key,
        "keywords": keywords,
        "subdistric": subdistric,
        "page": page,
        "offset": offset,
        "extensions": extensions,
        "filter": district_filter,
        "callback": callback,
        "output": output
    }
    r = requests.get(district_url,params=params)
    results = r.json()
    return results






# 搜索POI




# 关键字搜索
def poi_keywords(key,keywords,types=None,city=None,citylimit="false",children=0,offset=20,page=1,extensions="base",sig=None,output="json",callback=None):
    """关键字搜索"""
    poi_url = "https://restapi.amap.com/v3/place/text?parameters"
    params = {
        "key": key,
        "keywords": keywords,
        "types": types,
        "city": city,
        "citylimit": citylimit,
        "children": children,
        "offset": offset,
        "page": page,
        "extensions": extensions,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    r = requests.get(poi_url,params=params)
    results = r.json()
    return results


# 周边搜索



def poi_around(key,location,keywords=None,types="050000|070000|120000",city=None,radius=3000,sortrule="distance",offset=20,page=1,extensions="base",sig=None,output="json",callback=None):
    """在用户传入经纬度坐标点附近，在设定的范围内，按照关键字或POI类型搜索"""
    poi_around_url = "https://restapi.amap.com/v3/place/around?parameters"
    params = {
        "key":key,
        "location":location,
        "keywords":keywords,
        "types":types,
        "city":city,
        "radius":radius,
        "sortrule":sortrule,
        "offset":offset,
        "page":page,
        "extensions":extensions,
        "sig":sig,
        "output":output,
        "callback":callback
    }
    r = requests.get(poi_around_url,params=params)
    results = r.json()
    return results




# 步行路径规划

def walking(key,origin,destination,sig=None,output="json",callback=None):
    """步行路径规划 API 可以规划100KM以内的步行通勤方案，并且返回通勤方案的数据"""
    walking_url = "https://restapi.amap.com/v3/direction/walking?parameters"
    params = {
        "key":key,
        "origin":origin,
        "destination":destination,
        "sig":sig,
        "output":output,
        "callback":callback
    }
    r = requests.get(walking_url,params=params)
    results = r.json()
    return results


# 公交路径规划

def transit(key,origin,destination,city,cityd=None,extensions="base",strategy=0,nightflag=0,date=None,time=None,sig=None,output="json",callback=None):
    """公交路径规划 API 可以规划综合各类公共（火车、公交、地铁）交通方式的通勤方案，并且返回通勤方案的数据。"""
    transit_url = "https://restapi.amap.com/v3/direction/transit/integrated?parameters"
    params = {
        "key":key,
        "origin":origin,
        "destination":destination,
        "city":city,
        "cityd":cityd,
        "extensions":extensions,
        "strategy":strategy,
        "nightflag":nightflag,
        "date":date,
        "time":time,
        "sig":sig,
        "output":output,
        "callback":callback
    }
    r = requests.get(transit_url,params=params)
    results = r.json()
    return results


# 驾车路径规划

def driving(key,origin,destination,originid=None,destinationid=None,origintype=None,destinationtype=None,strategy=0,waypoints=None,avoidroad=None,province=None,number=None,cartype=0,ferry=0,roadaggregation="false",nosteps=0,sig=None,output="json",callback=None,extensions="base"):
    """规划以小客车、轿车通勤出行的方案，并且返回通勤方案的数据"""
    driving_url = "https://restapi.amap.com/v3/direction/driving?parameters"
    params = {
        "key": key,
        "origin": origin,
        "destination": destination,
        "originid": originid,
        "destinationid": destinationid,
        "origintype": origintype,
        "destinationtype": destinationtype,
        "strategy": strategy,
        "waypoints": waypoints,
        "avoidroad": avoidroad,
        "province": province,
        "number": number,
        "cartype": cartype,
        "ferry": ferry,
        "roadaggregation": roadaggregation,
        "nosteps": nosteps,
        "sig": sig,
        "output": output,
        "callback": callback,
        "extensions": extensions
    }
    r = requests.get(driving_url,params=params)
    results = r.json()
    return results


# 骑行路径规划

def bicycling(key,origin,destination):
    """骑行路径规划用于规划骑行通勤方案，规划时不会考虑路况；考虑天桥、单行线、封路等情况。"""
    bicycling_url = "https://restapi.amap.com/v4/direction/bicycling?parameters"
    params = {
        "key":key,
        "origin":origin,
        "destination":destination
    }
    r = requests.get(bicycling_url,params=params)
    results = r.json()
    return results


# 货车路径规划

def truck(key,origin,destination,size,originid=None,destinationid=None,origintype=None,destinationtype=None,diu=None,strategy=1,waypoints=None,height=1.6,width=2.5,load=10,weight=0.9,axis=2,province=None,number=None,cartype=0,showpolyline=1,nosteps=0):
    """货车限行分为两种：客观限制（道路客观条件产生的宽高重限制）和政策限行（行政部门规定的限制），路径规划时会根据传入参数分别处理。"""
    truck_url = "https://restapi.amap.com/v4/direction/truck?parameters"
    params = {
        "key":key,
        "origin":origin,
        "destination":destination,
        "originid":originid,
        "destinationid":destinationid,
        "origintype":origintype,
        "destinationtype":destinationtype,
        "diu":diu,
        "strategy":strategy,
        "waypoints":waypoints,
        "size":size,
        "height":height,
        "width":width,
        "load":load,
        "axis":axis,
        "province":province,
        "number":number,
        "cartype":cartype,
        "showpolyline":showpolyline,
        "nosteps":nosteps
    }
    r = requests.get(truck_url,params=params)
    results = r.json()
    return results


# 行政区域查询

def district(key,keywords=None,subdistrict=1,page=1,offset=20,extension="base",district_filter="adcode",callback=None,output="json"):
    """根据用户输入的搜索条件可以帮助用户快速的查找特定的行政区域信息"""
    district_url = "https://restapi.amap.com/v3/config/district?parameters"
    params = {
        "key":key,
        "keywords":keywords,
        "subdistrict":subdistrict,
        "page":page,
        "offset":offset,
        "extensions":extension,
        "filter":district_filter,
        "callback":callback,
        "output":output
    }
    r = requests.get(district_url,params=params)
    results = r.json()
    return results


# 关键字搜索

def poi_text(key,keywords,types=None,city=None,citylimit=None,children=0,offset=20,page=1,extensions="base",sig=None,output="json",callback=None):
    """通过用POI的关键字进行条件搜索，例如：肯德基、朝阳公园等；同时支持设置POI类型搜索，例如：银行"""
    poi_text_url = "https://restapi.amap.com/v3/place/text?parameters"
    params = {
        "key":key,
        "keywords":keywords,
        "types":types,
        "city":city,
        "citylimit":citylimit,
        "children":children,
        "offset":offset,
        "page":page,
        "extensions":extensions,
        "sig":sig,
        "output":output,
        "callback":callback
    }
    r = requests.get(poi_text_url,params=params)
    results = r.json()
    return results


# 周边搜索

def poi_around(key,location,keywords=None,types="050000|070000|120000",city=None,radius=3000,sortrule="distance",offset=20,page=1,extensions="base",sig=None,output="json",callback=None):
    """在用户传入经纬度坐标点附近，在设定的范围内，按照关键字或POI类型搜索"""
    poi_around_url = "https://restapi.amap.com/v3/place/around?parameters"
    params = {
        "key":key,
        "location":location,
        "keywords":keywords,
        "types":types,
        "city":city,
        "radius":radius,
        "sortrule":sortrule,
        "offset":offset,
        "page":page,
        "extensions":extensions,
        "sig":sig,
        "output":output,
        "callback":callback
    }
    r = requests.get(poi_around_url,params=params)
    results = r.json()
    return results


# 多边形搜索
def polygon(key,polygon,keywords,types=None,offset=20,page=1,extensions="base",sig=None,output="json",callback=None):
    """多边形搜索"""
    polygon_url = "https://restapi.amap.com/v3/place/polygon?parameters "
    params = {
        "key": key,
        "location": location,
        "keywords": keywords,
        "types": types,
        "offset": offset,
        "page": page,
        "extensions": extensions,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    r = requests.get(poi_polygon_url,params=params)
    results = r.json()
    return results



# ID查询
def detail(key,id,sig=None,output="json",callback=None):
    """ID查询"""
    detail_url = "https://restapi.amap.com/v3/place/detail?parameters"
    params = {
        "key": key,
        "id": id,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    r = requests.get(poi_id_url,params=params)
    results = r.json()
    return results




# IP定位





# IP定位
def ip(key,ip,sig=None,output="json"):
    """根据用户输入的IP地址，能够快速的帮用户定位IP的所在位置"""
    ip_url = "https://restapi.amap.com/v3/ip?parameters"
    params = {
        "key": key,
        "ip": ip,
        "sig": sig,
        "output": output}
    r = requests.get(ip_url,params=params)
    results = r.json()
    return results






# 静态地图





def staticmap(key,location,zoom=15,size="400*400",scale=1,markers=None,labels=None,paths=None,traffic=0,sig=None):
    """静态地图"""
    staticmap_url = "https://restapi.amap.com/v3/staticmap?parameters"
    params = {
        "key": key,
        "location": location,
        "zoom": zoom,
        "size": size,
        "scale": scale,
        "markers": markers,
        "labels": labels,
        "paths": paths,
        "traffic": traffic,
        "sig": sig
    }
    r = requests.get(staticmap_url, params)
    data = Image.open(BytesIO(r.content))
    return data




# 坐标转换





def convert(key,location,coordsys="autonavi",sig=None,output="json"):
    """坐标转换"""
    convert_url = "https://restapi.amap.com/v3/assistant/coordinate/convert?parameters"
    params = {
        "key": key,
        "locations": location,
        "coordsys": coordsys,
        "sig": sig,
        "output": output}
    r = requests.get(coordinate_convert_url,params=params)
    results = r.json()
    return results




# 天气查询





def weather(key,city,extension="all",output="json"):
    """天气查询"""
    weather_url = "https://restapi.amap.com/v3/weather/weatherInfo?parameters"
    params = {
        "key": key,
        "city": city,
        "extensions":extension,
        "output": output
    }
    r = requests.get(weather_url,params=params)
    results = r.json()
    return results





# 输入提示





def input_tips(key,keyword,poi_type=None,location=None,city=None,citylimit="false",datatype="all",sig=None,output="json",callback=None):
    """输入提示"""
    input_tips_url = "https://restapi.amap.com/v3/assistant/inputtips?parameters"
    params = {
        "key": key,
        "keywords": keyword,
        "type": poi_type,
        "location": location,
        "city": city,
        "citylimit": citylimit,
        "datatype": datatype,
        "sig": sig,
        "output": output,
        "callback": callback
    }
    r = requests.get(input_tips_url,params=params)
    results = r.json()
    return results




# 地理围栏





def creat_geofence():
    creat_geofence_url = 'https://restapi.amap.com/v4/geofence/meta?key={}'
    params = {
        "name":"测试围栏",
        "points":"116.670040,23.468340;116.669649,23.466642;116.671216,23.466342;116.672627,23.468448;",
        "enable":"true",
        "repeat": "Mon,Tues,Wed,Thur,Fri,Sat,Sun",
#         "valid_time":"2020-4-23"
    }
    r = requests.get(creat_geofence_url,params=params)
    results = r.json()
    return results



# 指定线路交通态势

def traffic_road(key,name,city,adcode=None,level=5,extensions="base",sig=None,output="json",callback=None):
    """提供根据用户输入的内容能够返回希望查询的交通态势情况"""
    traffic_road_url = "https://restapi.amap.com/v3/traffic/status/road?parameters"
    params = {
        "key":key,
        "name":name,
        "city":city,
        "adcode":adcode,
        "level":level,
        "extensions":extensions,
        "sig":sig,
        "output":output,
        "callback":callback
    }
    r = requests.get(traffic_road_url,params=params)
    results = r.json()
    return results






